package com.example.demo.mapper;

import com.example.demo.entity.Instrument;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;
import java.util.List;

@Mapper
public interface InstrumentMapper {

    @Insert("INSERT INTO instrument_tb (instrument_id, name, type, image, audio, description, cultural, music_id, view_count)" +
            "VALUES (#{instrument_id}, #{name}, #{type}, #{image}, #{audio}, #{description}, #{cultural}, #{music_id}, #{view_count})")
    void insertInstrument(Instrument instrument);

    @Update("UPDATE instrument_tb SET name = #{name}, type = #{type}, image = #{image}, audio = #{audio}, " +
            "description = #{description}, cultural = #{cultural}, music_id = #{music_id}, view_count = #{view_count}, " +
            "create_time = #{create_time} WHERE instrument_id = #{instrument_id}")
    void updateInstrument(Instrument instrument);

    @Delete("DELETE FROM instrument_tb WHERE instrument_id = #{instrument_id}")
    void deleteInstrument(@Param("instrument_id") Integer instrumentId);

    @Select("SELECT * FROM instrument_tb WHERE instrument_id = #{instrument_id}")
    Instrument selectInstrumentById(@Param("instrument_id") Integer instrumentId);

    @Select("SELECT * FROM instrument_tb")
    List<Instrument> selectAllInstruments();

    @Select("SELECT * FROM instrument_tb WHERE name LIKE CONCAT('%', #{name}, '%')")
    List<Instrument> searchInstruments(@Param("name") String name);

    @Select("SELECT * FROM instrument_tb ORDER BY view_count DESC LIMIT 5")
    List<Instrument> findHotInstruments();

    // 更新乐器的浏览量
    @Update("UPDATE instrument_tb SET view_count = view_count + 1 WHERE instrument_id = #{id}")
    void incrementViewCount(Integer id);
}